<template>
  <el-form
    ref="ruleForm"
    :model="ruleForm"
    :rules="rules"
    label-width="150px"
    style="width: 60%"
  >
    <el-form-item :label="$t('oldPassword')" prop="password">
      <el-input
        v-model="ruleForm.password"
        type="password"
        autocomplete="off"
      />
    </el-form-item>
    <el-form-item :label="$t('newPassword1')" prop="newpassword1">
      <el-input
        v-model="ruleForm.newpassword1"
        type="password"
        autocomplete="off"
      />
    </el-form-item>
    <el-form-item :label="$t('newPassword2')" prop="newpassword2">
      <el-input
        v-model="ruleForm.newpassword2"
        type="password"
        autocomplete="off"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitPasswordForm('ruleForm')">
        {{ $t('submit') }}</el-button>
      <el-button @click="resetForm('ruleForm')">{{ $t('reset') }}</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import store from '@/store';
import { mapGetters } from 'vuex';
export default {
  data() {
    var validateOldPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error(this.$t('inputOldPassword')));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error(this.$t('inputPassword')));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('newpassword2');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error(this.$t('inputPasswordAgain')));
      } else if (value !== this.ruleForm.newpassword1) {
        callback(new Error(this.$t('inputPasswordInconsistent')));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        password: '',
        newpassword1: '',
        newpassword2: ''
      },
      // 表单校验
      rules: {
        password: [{ validator: validateOldPass, trigger: 'blur' }],
        newpassword1: [{ validator: validatePass, trigger: 'blur' }],
        newpassword2: [{ validator: validatePass2, trigger: 'blur' }]
      }
    };
  },
  computed: {
    ...mapGetters(['username'])
  },
  methods: {
    submitPasswordForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          var passwordInfo = {
            user_name: this.username,
            old_password: this.ruleForm.password,
            new_password: this.ruleForm.newpassword1
          };
          var resp = await store.dispatch(
            'user/updateUserPassword',
            passwordInfo
          );

          if (resp.success) {
            this.$alert(this.$t('passwordResetOK'), this.$t('modifyPassword'), {
              confirmButtonText: this.$t('confirm'),
              type: 'success',
              callback: async() => {
                await store.dispatch('user/logout');
                location.reload();
              }
            });
          } else {
            this.targaMessageBox(this, resp.msg);
          }
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>
